package wedaex.helper;

import java.util.ArrayList;
import java.util.List;


/**
 * TODO to be used in recognizing cross element target string
 * 
 * @author cedricho
 *
 */
public class HtmlDisplayString {
	private List<Integer> pos;
	private static char[] buffer;
	
	public static HtmlDisplayString createHtmlDisplayString(String htmlContent) {
		int openBraceCount = 0;

		List<Integer> pos = new ArrayList<Integer>();
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < htmlContent.length(); i++) {
			char c = htmlContent.charAt(i);
			
			if(c=='<') openBraceCount++;
			else if(c=='>') openBraceCount = Math.max(openBraceCount-1, 0);
			else if(openBraceCount>0) continue;	// ignore
			
			// append the displayed character
			pos.add(i);
			sb.append(c);
		}
		return new HtmlDisplayString(pos, sb.toString().toCharArray());
	}
	
	public HtmlDisplayString(List<Integer> pos, char[] buffer) {
		this.pos = pos;
		this.buffer = buffer;
	}

	public String toString() {
		return new String(buffer);
	}
}
